import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/login.vue'
import pathArr from './pathArr.js'
import HeRouter from './he/he.js'
import DengRouter from './deng/deng.js'
import HuRouter from './hu/hu.js'
import LuogRouter from './luo/luo.js'
import YanRouter from './yan/yan.js'
import ZhaoRouter from './zhao/zhao.js'
import ZhangRouter from './zhang/zhang.js'

Vue.use(VueRouter)

const routes = [{
  path: '/',
  name: 'Login',
  redirect: '/login',
  component: Login,
},
{
  path: '/login',
  name: 'Login',
  component: () =>
    import('../views/login.vue')
},
{
  path: "/main",
  name: "main",
  component: () =>
    import('../views/Main.vue'),
  children: [

    ...HeRouter,
    ...DengRouter,
    ...HuRouter,
    ...LuogRouter,
    ...YanRouter,
    ...ZhaoRouter,
    ...ZhangRouter,
    {
      path: "",
      component: () => import('../views/hu/ArticleType.vue')
    }
  ]
},
  /* {
       path:"",
       component: () => import('../views/TeamManagement.vue')
     } */
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//全局前置守卫，判断是否有token来判断是否有权限访问主页
 router.beforeEach(function (to, from, next) {
  if (pathArr.indexOf(to.path) !== -1) {//如果能够在路径文件中找到对应的路径
    const token = sessionStorage.getItem('token');//获取token
    // console.log('全局前置守卫',token);
    if (token) {//如果存在token  表示已经登录
      next();
    } else {//表示未登录
      next('/login');
    }
  } else {
    next();
  }
}) 
export default router